package com.yun.zf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.common.admin.model.sys.SysDictDataAddMo;
import com.yun.zf.common.admin.model.sys.SysdictDataEditMo;
import com.yun.zf.common.mapstruct.admin.SysMapper;
import com.yun.zf.system.mapper.SysDictDataMapper;
import com.yun.zf.system.service.ISysDictDataService;
import com.yun.zf.common.admin.domain.SysDictData;
import com.yun.zf.common.admin.model.SysDicDataMo;
import com.yun.zf.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 字典 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService
{
    @Autowired
    private SysDictDataMapper dictDataMapper;



    /**
     * 根据字典类型查询字典数据
     * 
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public IPage<SysDictData> selectDictDataByType(SysDicDataMo dictType)
    {
        QueryWrapper<SysDictData> dictDataQueryWrapper = new QueryWrapper<>();
      LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = dictDataQueryWrapper.lambda().eq(SysDictData::getDictType,dictType.getDictType())
        .eq(!("all").equals(dictType.getStatus()),SysDictData::getStatus,dictType.getStatus())
        .eq(StringUtils.isNotEmpty(dictType.getDictLabel()),SysDictData::getDictLabel,dictType.getDictLabel());
        lambdaQueryWrapper.orderBy(StringUtils.isNotNull(dictType.getSort().getUpdateTime()),("ascend").equals(dictType.getSort().getUpdateTime()), SysDictData::getUpdateTime);
        Page<SysDictData> page = new Page<>(dictType.getCurrent(), dictType.getPageSize());
        IPage<SysDictData> dicPage = baseMapper.selectPage(page, lambdaQueryWrapper);
        return  dicPage;


    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     * 
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode)
    {
        QueryWrapper<SysDictData> dictDataQueryWrapper = new QueryWrapper<>();
        dictDataQueryWrapper.lambda().eq(SysDictData::getDictCode,dictCode);
        return baseMapper.selectOne(dictDataQueryWrapper);
    }

    /**
     * 通过字典ID删除字典数据信息
     * 
     * @param dictCode 字典数据ID
     * @return 结果
     */
    @Override
    public int deleteDictDataById(Long dictCode)
    {
        return dictDataMapper.deleteDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     * 
     * @param dictCodes 需要删除的字典数据ID
     * @return 结果
     */
    public void deleteDictDataByIds(Long[] dictCodes)
    {

         dictDataMapper.deleteDictDataByIds(dictCodes);
    }

    /**
     * 新增保存字典数据信息
     * 
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictDataAddMo dictData)
    {
      SysDictData sysDictData =   SysMapper.INSTANCE.moToData(dictData);
        return baseMapper.insert(sysDictData);
    }

    /**
     * 修改保存字典数据信息
     * 
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysdictDataEditMo editMo)
    {
        QueryWrapper<SysDictData> dictDataQueryWrapper = new QueryWrapper<>();
        dictDataQueryWrapper.lambda().eq(SysDictData::getDictCode,editMo.getDictCode());
        SysDictData dictData =    baseMapper.selectOne(dictDataQueryWrapper);
        dictData.setDictLabel(editMo.getDictLabel());
        dictData.setDictKey(editMo.getDictKey());
        dictData.setDictValue(editMo.getDictValue());
        dictData.setDictSort(editMo.getDictSort());
        dictData.setStatus(editMo.getStatus());
        dictData.setRemark(editMo.getRemark());
        dictData.setUpdateTime(new Date());
        return   baseMapper.update(dictData,dictDataQueryWrapper);
    }

    @Override
    public void updateDicDataByType(String oldType, String newType) {

        dictDataMapper.updateDictDataType(oldType,newType);
    }

    @Override
    public String getValue(String dictType, String dictKey) {
        QueryWrapper<SysDictData> dictDataQueryWrapper = new QueryWrapper<>();
        dictDataQueryWrapper.select("dict_value").eq("dict_type",dictType)
        .eq("dict_key",dictKey);
        SysDictData dictData = baseMapper.selectOne(dictDataQueryWrapper);
        if (dictData!=null){
            return  dictData.getDictValue();
        }
        return null;
    }

    @Override
    public int hasDataByType(String dictType) {
       QueryWrapper<SysDictData> dataQueryWrapper = new QueryWrapper<>();
       dataQueryWrapper.lambda().eq(SysDictData::getDictType,dictType);
       return  baseMapper.selectCount(dataQueryWrapper);
    }
}
